package com.boe.csb.web.controller.csb;

import com.alibaba.csb.console.sdk.Result;
import com.alibaba.fastjson.JSONObject;
import com.boe.csb.core.common.BoeEmailContentConfig;
import com.boe.csb.core.common.CsbConstants;
import com.boe.csb.core.common.exception.CSBErrorCode;
import com.boe.csb.core.common.exception.CSBException;
import com.boe.csb.core.common.utils.CsbApiCaller;
import com.boe.csb.core.entity.UserInfo;
import com.boe.csb.core.service.SendMailService;
import com.boe.csb.core.service.csb.CsbManagerApiService;
import com.boe.csb.core.service.csb.CsbManagerOrderService;
import com.boe.csb.web.controller.BaseController;
import com.boe.csb.web.model.ResultModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import java.util.List;

/**
 * Created by tianxiang.luo on 16/11/26.
 * 1.订阅服务
 * 2.查看自己订阅的
 * 2.删除自己订阅
 */

@RequestMapping("/api/manager/order")
@RestController
public class CsbManagerOrderController extends BaseController {

    @Autowired
    private CsbManagerOrderService csbManagerOrderService;
   

    /**
     * 服务订阅
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "service", method = RequestMethod.POST)
    public ResultModel<Result> subscribe(HttpSession session, @RequestBody JSONObject data) {
        UserInfo userInfo = fetchUserInfo(session);
        if(userInfo.getIsAdmin()!=null && userInfo.getIsAdmin()){
            ResultModel.fail(CSBErrorCode.USER_IS_ADMIN,"没有权限订阅");
        }
        
        return ResultModel.success(csbManagerOrderService.subscribeService(String.valueOf(userInfo.getUserId()), data));
    }

    /**
     * 自己订阅过的服务
     */
    @RequestMapping(value = "/own", method = RequestMethod.GET)
    public ResultModel<Result> ownSubscribeServices(HttpSession session, @RequestParam Integer pageNum,
                                                    @RequestParam(required = false) Boolean showDelStatus,
                                                    @RequestParam(required = false) String serviceName,
                                                    @RequestParam(required = false)String alias,
                                                    @RequestParam(required = false)String status) {
        String userId = fetchUserId(session);
        return ResultModel.success(csbManagerOrderService.ownSubscribedService(userId, pageNum, showDelStatus, serviceName,alias,status));
    }


    /**
     * 删除自己订阅的服务
     */
    @RequestMapping(value = "/delete/own", method = RequestMethod.POST)
    public ResultModel<Result> deleteOwn(HttpSession session, @RequestParam List<Long> idList) {
        String userId = fetchUserId(session);
        return ResultModel.success(csbManagerOrderService.deleteOrders(userId, idList));
    }


}
